package com.example.fitnessservice.module.dao.courseMapper;

import com.example.fitnessservice.module.pojo.Coach;
import com.example.fitnessservice.module.pojo.CourseDetails;
import com.example.fitnessservice.module.pojo.CourseDetailsCamp;
import com.example.fitnessservice.module.pojo.CourseType;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @author feng
 * @version 1.0
 * @&#064;description: TODO
 * @&#064;date 2022/11/22 15:04
 */
@Mapper
public interface CourseMapper {
/*
查询某个场地所有的某个类型的课程
 */
    @Select({"<script>",
            "select * from course_details where place_id=#{placeId} and course_type=#{courseType}",
            "<when test='type==1'>", "and course_start_time <![CDATA[>]]> NOW()", "</when>",
            "<when test='type==2'>", "and course_start_time <![CDATA[<=]]>NOW() and course_end_time<![CDATA[=>]]>NOW()", "</when>",
            "<when test='type==3'>", "and course_end_time <![CDATA[<]]> NOW()", "</when>",
            "</script>"
    })
    List<CourseDetails> findTypeCourse(@Param("placeId") Integer placeId, @Param("courseType") Integer courseType,@Param("type")Integer type);
/*
查询某个场地的所有课程(不包括训练营)
 */
@Results({
        @Result(id = true, property = "id", column = "id"),
        @Result(property = "courseName", column = "course_name"),
        @Result(property = "placeId", column = "place_id"),
        @Result(property = "courseType", column = "course_type"),
        @Result(property = "courseStartTime", column = "course_start_time"),
        @Result(property = "courseEndTime", column = "course_end_time"),
        @Result(property = "courseStatus", column = "course_status"),
        @Result(property = "courseDescription", column = "course_description"),
        @Result(property = "courseMoney", column = "course_money"),
        @Result(property = "courseNum", column = "course_num"),
        @Result(property = "courseCoachId", column = "course_coach_id"),
        @Result(property = "courseCoach", column = "course_coach_id",
                javaType = Coach.class,
                one = @One(select = "com.example.fitnessservice.module.dao.coachmapper.CoachMapper.findCoachById"))})
@Select("select * from course_details where place_id=#{placeId} and course_type=#{type}")
      List<CourseDetails> findOnePlaceCourse(@Param("placeId") Integer placeId, @Param("type") Integer type);

    @Select({
            "<script>",
            "select * from course_details",
            "<when test='type==2'>", "and course_start_time &gt; NOW()", "</when>",
            "<when test='type==4'>",
            "and course_start_time &lt; NOW() and course_end_time &gt; NOW() or course_start_time = NOW() or course_end_time = NOW()",
            "</when>",
            "<when test='type==1'>", "and course_end_time &lt; NOW()", "</when>",
            "</script>"
    })
    List<CourseDetails> findAllPlaceCourse(Integer type);

    /*
        &lt;	<	小于
    &gt;	'>'	大于
    &amp;	&	和号
    &apos;	'	单引号
    &quot;	"	双引号
     */
    @Select("select * from course_details where id =#{id}")
    CourseDetails findCourseDetailsById(Integer id);

    @Update("update course_details set course_num=course_num+1 where id=#{courseId}")
    Integer numAdd(Integer courseId);

    @Update("update course_details set course_num=course_num-1 where id=#{courseId}")
    Integer numReduce(Integer courseId);

    /**
     * 根据场馆的id，查询课程
     *
     * @param placeId
     * @return
     */
    @Results({
            @Result(id = true, property = "id", column = "id"),
            @Result(property = "courseName", column = "course_name"),
            @Result(property = "placeId", column = "place_id"),
            @Result(property = "courseType", column = "course_type"),
            @Result(property = "courseStartTime", column = "course_start_time"),
            @Result(property = "courseEndTime", column = "course_end_time"),
            @Result(property = "courseStatus", column = "course_status"),
            @Result(property = "courseDescription", column = "course_description"),
            @Result(property = "courseMoney", column = "course_money"),
            @Result(property = "courseNum", column = "course_num"),
            @Result(property = "courseCoachId", column = "course_coach_id"),
            @Result(property = "courseCoach", column = "course_coach_id",
                    javaType = Coach.class,
                    one = @One(select = "com.example.fitnessservice.module.dao.coachmapper.CoachMapper.findCoachById"))})

    @Select("select * from course_details where place_id = #{placeId}")
    public List<CourseDetails> findCourseByPlaceId(Integer placeId);

    /**
     * 根据场馆的id，查询还未开始和正在进行的训练营
     *
     * @param placeId
     * @return
     */
    @Results({
            @Result(id = true, property = "id", column = "id"),
            @Result(property = "courseId" ,column = "course_id"),
            @Result(property = "courseName", column = "course_name"),
            @Result(property = "placeId", column = "place_id"),
            @Result(property = "courseStartTime", column = "course_start_time"),
            @Result(property = "courseEndTime", column = "course_end_time"),
            @Result(property = "courseStatus", column = "course_status"),
            @Result(property = "courseDescription", column = "course_description"),
            @Result(property = "courseMoney", column = "course_money"),
            @Result(property = "courseNum", column = "course_num"),
            @Result(property = "courseCoachId", column = "course_coach_id"),
            @Result(property = "courseCoach", column = "course_coach_id",
                    javaType = Coach.class,
                    one = @One(select = "com.example.fitnessservice.module.dao.coachmapper.CoachMapper.findCoachById"))})
    @Select("select * from course_details_camp where place_id = #{placeId}")
    public List<CourseDetailsCamp> findCampByPlaceId(Integer placeId);
    @Select("select * from course_type_train where id = #{id}")
    public CourseType findCourseTypeById(Integer id);

    @Update("update course_details set course_status=4 where id=#{courseId} and course_status=2 and course_start_time<NOW() and course_end_time>NOW() ")
    Integer takeCourse(Integer courseId);

    @Select("select * from course_details where id=#{courseId} and course_start_time<NOW() and course_end_time>NOW()")
    CourseDetails findNowCourse(Integer courseId);

    @Select("select * from course_details_camp where course_id = #{id}")
    public CourseDetailsCamp findCampByCourseId(Integer id);

    @Select("select course_status from user_course where user_id = #{userId} and course_id = #{courseId} and course_camp_if = #{courseCampIf}")
    Integer getStatusByUserIdAndCourseId(@Param("userId") Integer userId, @Param("courseId") Integer courseId, @Param("courseCampIf") Integer courseCampIf);
}
